#ifndef STATION_PROTOCOL_H
#define STATION_PROTOCOL_H

/******************* Station Config *******************/
#include "ct_type.h"

// #define USE_MOUDLE_8_STATION
//  #define USE_MOUDLE_4_STATION

// #if defined(USE_MOUDLE_8_STATION)
// #define STATION_MODULE_TYPE MODULE_TYPE_8_STATION
// #define MAX_STATION_NUMBER  8 /**< Maximum stations for 8-station module. */
// #elif defined(USE_MOUDLE_4_STATION)
// #define STATION_MODULE_TYPE MODULE_TYPE_4_STATION
// #define MAX_STATION_NUMBER  4 /**< Maximum stations for 4-station module. */
// #endif
/******************* Station Config *******************/

// Define the protocol constants
#define STATION_PROTOCOL_HEADER      0x5A
#define STATION_PROTOCOL_HEADER_LENGTH 5u
#define STATION_PROTOCOL_MAX_LENGTH  40u
#define STATION_PROTOCOL_TIMEOUT_CNT 250u    // Timeout count value in ms
// Define the checksum size
#define STATION_CHECKSUM_SIZE 1u

// Define the maximum station number
#define MAX_STATION_NUMBER   8 /**< Maximum stations for 8-station module. */
#define MAX_4_STATION_NUMBER 4 /**< Maximum stations for 4-station module. */
#define MAX_8_STATION_NUMBER 8 /**< Maximum stations for 8-station module. */

// Define the maximum data length
#define STATION_FRAME_MAX_LENGTH  (STATION_PROTOCOL_MAX_LENGTH + 5u)
#define STATION_MAX_RETRIES      3u      //
#define STATION_RESPONSE_TIMEOUT 250u    // Timeout in ms,based on 1ms tick

// Define the command words

typedef enum {
    MODULE_TYPE_4_STATION      = (uint8_t)1,
    MODULE_TYPE_8_STATION      = (uint8_t)2,
    MODULE_TYPE_2_WIRE_DECODER = (uint8_t)3
} ModuleType_t;

typedef enum {
    STATION_CMD_OPEN_STATION        = (uint8_t)0x01,
    STATION_CMD_CLOSE_STATION       = (uint8_t)0x02,
    STATION_CMD_UPLOAD_TYPE         = (uint8_t)0x08,
    STATION_CMD_ALLOCATE_STATION_NO = (uint8_t)0x09,
    STATION_CMD_HOST_RESPONSE       = (uint8_t)0x15,
    STATION_CMD_SLAVE_RESPONSE      = (uint8_t)0x06
} StationCommand_t;

// UART state machine states
typedef enum {
    WAIT_FOR_HEADER,
    WAIT_FOR_LENGTH,
    WAIT_FOR_DATA,
} STATION_UART_STATE;

// Station read error codes
typedef enum {
    STATION_READ_SUCCESS = 0,         // Successfully read packet
    STATION_READ_ERROR_INCOMPLETE,    // Incomplete packet
    STATION_READ_ERROR_HEADER,        // Header error
    STATION_READ_ERROR_LENGTH,        // Length field error
    STATION_READ_ERROR_OVERFLOW,      // Buffer overflow
    STATION_READ_ERROR_CHECKSUM,      // Checksum error
    STATION_READ_ERROR_TIMEOUT,      // Timeout
    STATION_READ_ERROR_OTHER,         // Other error
    STATION_READ_NO_ERROR             // No error
} StationReadErrorCode_t;

// Station state machine states
typedef enum {
    STATION_POWER_ON = 0,      // Power on state
    INITIALIZE_MODULE,         // Initialize module state
    WAIT_FOR_HOST_RESPONSE,    // Wait for host response state
    CHECK_RESPONSE,            // Check response state
    WAIT_FOR_HOST_COMMAND,     // Wait for host command state
    EXECUTE_ACTION,            // Execute action state
    SLAVE_RESPONSE,            // Slave response state
    RESEND_PACKET,             // Resend packet state
    RESTART_MODULE,            // Restart module state
    ERROR_HANDLING,            // Error handling state
    STATION_END                // Station end state
} StationState;

typedef struct {
    uint8_t          SerialNum;     // Serial number of the station
    StationCommand_t cmd;           // Command word
    uint8_t         DataBuff[STATION_PROTOCOL_MAX_LENGTH];      // Pointer to the data buffer
    uint8_t          DataLength;    // Length of the data
    uint8_t          CheckSum;
    uint8_t          rx_cmd;
    uint8_t          rx_checksum;    // Checksum of the data
    uint8_t          rx_bReady;      // Ready flag for the receive buffer
} StationPacket_t;

extern StationPacket_t Packet;

// Function prototypes
void Station_Init(void);
void Station_MainLoop(void);
#endif    // STATION_PROTOCOL_H

